#!/bin/sh
# Este script debe permitirnos verificar el estado y el cumplimiento de los
# requisitos de los paquetes instalados.


# Valida los scripts del directorio especificado
validate_odscripts() {
	for i in $1/*.sh
	do
		if test -f $i; then
			if test -x $i; then
				echo "   Checking $i"
				grep -q "^#desc" $i || echo "WARNING: #desc not found in $i"
				grep -q "^#type" $i || echo "WARNING: #type not found in $i"
				grep -q "^#package" $i || echo "WARNING: #package not found in $i"
			else
				echo "WARNING: $i is not executable"
			fi
		fi
	done
}

if test -f $1 
then
	mkdir -p tmp
	if tar -zxf $1 -C tmp
	then
		BN=`basename $1`
		CODE=`echo $BN | cut -f1 -d-`
		echo "Validating $BN ($CODE) ..."
		# Revisamos los scripts de /usr/local/opendomo y subdirectorios
		ODDIR="tmp/usr/local/opendomo"
		if test -d $ODDIR/services; then
			validate_odscripts $ODDIR/services
			for d in $ODDIR/services/* $ODDIR/services/*/* $ODDIR/services/*/*/* 
			do
				if test -d $d; then
					validate_odscripts $d
				fi
			done
		fi
		
		# Comprobamos que no usa el antiguo "install.sh"
		if test -f tmp/install.sh; then
			echo "WARNING: old install.sh file structure. DEPRECATED"
		fi
		if test -f tmp/var/pkg/${CODE}_install.sh; then
			if ! test -f tmp/var/pkg/${CODE}_uninstall.sh; then
				echo "WARNING: install.sh without uninstall.sh"
			fi
		fi
	
		# Nos aseguramos que no hay ningún archivo en /usr/local/opendomo/
		for f in tmp/usr/local/opendomo/*.sh 
		do
			if test -f $f && ! test -L $f; then
				echo "WARNING: $f should not exist!"
			fi
		done
	
		if test -d tmp/etc/init.d/; then
			for f in tmp/etc/init.d/*; do
				if test -f $f; then
				echo " Checking service $f ..."
				grep -q "^#desc" $f || echo "WARNING: $f is missing #desc"
				grep -q "start)" $f || echo "WARNING: $f does not support 'start'" 
				grep -q "stop)" $f || echo "WARNING: $f does not support 'stop'" 
				grep -q "status)" $f || echo "WARNING: $f does not support 'status'"
				grep -q "restart)" $f || echo "WARNING: $f does not support 'restart'"  
				fi
			done
		fi
	else
		echo "ERROR: seems that $1 is not a TAR.GZ valid file!"
	fi
	rm -fr tmp
else
	echo "ERROR: The specified module does not exist!"
fi

